package com.fanshuai.tree.quadtree;

public class Rect {
    public long x1, y1, x2, y2;

    public Rect(long x1, long y1, long x2, long y2) {
        this.x1 = x1;
        this.y1 = y1;
        this.x2 = x2;
        this.y2 = y2;
    }

    /**
     * 2矩形是否相交
     * @param r
     * @return
     */
    public boolean intersect(Rect r) {
        return null != r && !(r.x2 < x1 || r.x1 > x2 || r.y1 > y2 || r.y2 < y1);
    }

    /**
     * 矩形是否包含点(x, y)
     * @param x
     * @param y
     * @return
     */
    public boolean contains(long x, long y) {
        return (x >= x1 && x <= x2) && (y >= y1 && y <= y2);
    }

    /**
     * 是都包含矩形rect
     * @param rect
     * @return
     */
    public boolean contains(Rect rect) {
        return rect != null && contains(rect.x1, rect.y1) && contains(rect.x2, rect.y2);
    }

    @Override
    public String toString() {
        return String.format("(%d, %d) -> (%d, %d)", x1, y1, x2, y2);
    }
}
